package com.gamblord.coinflip;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.os.Build;

public class MainActivity extends Activity {
	
	private ImageView headsImage;
	private ImageView tailsImage;
	
	private boolean isHeads = true;
	private boolean mActive = false;
	private boolean mSwap = false;
	
	public static final int MDELAY = 200;
	private long lastUpdate = 0;
	
	Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		setContentView(R.layout.fragment_main);
/*
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
*/
		
		mHandler = new Handler(Looper.getMainLooper());
				
		headsImage = (ImageView) findViewById(R.id.coinHeads);
		tailsImage = (ImageView) findViewById(R.id.coinTails);
		tailsImage.setVisibility(View.GONE);

		headsImage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				if(lastUpdate == 0) {
					lastUpdate = System.currentTimeMillis();
				}
				else {				
					if(System.currentTimeMillis() - lastUpdate <= (MDELAY * 8))
						return;
				}
				mActive = !mActive;
				mHandler.post(multiFlips);
			}
		});
	
	}

	private Runnable multiFlips = new Runnable() 
    {
        public void run() 
        {
	        if (mActive) {
				if (isHeads) {
	                //Run your flip animation here
	                applyRotation(0, 90);
	                isHeads = !isHeads;
	                mHandler.postDelayed(this, MDELAY * 2);
	            } else {
	                //Run your flip animation again
	                applyRotation(0, 90);
	                isHeads = !isHeads;
	                mHandler.postDelayed(this, MDELAY * 2);
	            }
	        }
        }        
    };	
	
    
    public int getDelay() {
    	return MDELAY;
    }
    
	private void applyRotation(float start, float end) {
		// Find the center of image
		final float centerX = headsImage.getWidth() / 2.0f;
		final float centerY = headsImage.getHeight() / 2.0f;
		
		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip3DAnimation rotation =
				new Flip3DAnimation(start, end, centerX, centerY);
		rotation.setDuration(MDELAY);
		rotation.setFillAfter(true);
		//rotation.setInterpolator(
		//		new AccelerateInterpolator());
		rotation.setInterpolator(
						new LinearInterpolator());
		rotation.setAnimationListener(
				new DisplayNextView(isHeads, headsImage, tailsImage));
		
		if (isHeads) {
			headsImage.startAnimation(rotation);
		}
		else {
			tailsImage.startAnimation(rotation);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}
